<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" isELIgnored="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">


    <title>添加作品</title>
    <meta name="keywords" content="">
    <meta name="description" content="">
    <link rel="shortcut icon" href="favicon.ico"> 
    <link href="${pageContext.request.contextPath }/css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="${pageContext.request.contextPath }/css/font-awesome.css?v=4.4.0" rel="stylesheet">
    <!-- jqgrid-->
    <link href="${pageContext.request.contextPath }/css/plugins/jqgrid/ui.jqgrid.css?0820" rel="stylesheet">
    <link href="${pageContext.request.contextPath }/css/animate.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath }/css/style.css?v=4.1.0" rel="stylesheet">
    <script src="${pageContext.request.contextPath }/js/jquery.min.js?v=2.1.4"></script>
    <script src="${pageContext.request.contextPath }/js/bootstrap.min.js?v=3.3.6"></script>
    
    <!-- 自定义js -->
    <script src="${pageContext.request.contextPath }/js/content.js?v=1.0.0"></script>
    <!-- jQuery Validation plugin javascript-->
    <script src="${pageContext.request.contextPath }/js/plugins/validate/jquery.validate.min.js"></script>
    <script src="${pageContext.request.contextPath }/js/plugins/validate/messages_zh.min.js"></script>
    <script src="${pageContext.request.contextPath }/js/plugins/layer/layer.min.js"></script>
    
	<style>
		input,select{
			 width:168px;
			 height:25px;
			 border:1px solid #ccc;
			 margin-left:24px;
		}
		
		.form-item{
			display:flex;
		}
		.label-text{
		   height: 34px;
		   line-height: 34px;
		   font-weight: normal;
		}
		.addInput{ 
			background-color: #ffffff;
		    background-image: none;
		    border: 1px solid #e5e6e7;
		    border-radius: 1px;
		    color: inherit;
		    display: block;
		    font-size: 14px;
		    padding: 6px 12px;
		    transition: border-color 0.15s ease-in-out 0s, box-shadow 0.15s ease-in-out 0s;
		    height: 34px;
		   line-height: 34px;
		   flex: 1;
		}
		.saveBtn{
			 display: block;
		    margin: 15px auto;
		    width: 120px;
		}
	</style>
</head>

<body class="gray-bg">
    <div class="wrapper wrapper-content  animated fadeInRight">
        <div class="row">
            <div class="col-sm-6">
                <div class="social-feed-box">
                    <div class="social-footer">
						<form name = "form1" id = "add_proxy_ar"  enctype='multipart/form-data'>
							<div class="form-item">
								<label class="label-text">名称：</label>
								<input type = "text" class = "addInput" name = "name" maxlength="20"  placeholder="输入名称，不超过20字" ><br/><br/>
							</div>	
							<div class="form-item">
								<label  class="label-text">视频：</label>
								<input type='file' name='videoUrls' onchange="videoChange(this);" accept='.mp4'>
								&nbsp;&nbsp;只支持.mp4格式
							</div>	
					    	<button id = 'button_id' class="btn btn-primary saveBtn" type="submit">保存</button>
					    </form>
						<div id="doing"></div> 
                    </div>
                </div>
            </div>
        </div>
    </div>

</body>

    
    <script type="text/javascript">
 
    $().ready(function () {
        var icon = "<i class='fa fa-times-circle'></i> ";
        $("#add_proxy_ar").validate({
            rules: {
            	name: {//姓名
                    required: true,
    				maxlength: 20
                }
            },
            messages: {
            	name:{
            		required:icon + "请填写识别图名称",
            		maxlength:icon + "最大长度为20"
                }
                
            },
    		submitHandler: function(form) {
    			insertProxyAr();
    		}
        });
    });

    //上传图片
    function imageChange(target) {
        var fileSize = 0;
        fileSize = target.files[0].size;
        var size = fileSize / 1024;
        if(size > 2000){
           alert("图片不能大于2M");
           target.value="";
           return false;   //阻止submit提交
        }
        var name=target.value;
        var fileName = name.substring(name.lastIndexOf(".")+1).toLowerCase();
        if(fileName !="jpg" && fileName !="jpeg" && fileName !="png"){
           alert("请选择图片格式文件上传(jpg,jpeg,png)！");
           target.value="";
           return false;   //阻止submit提交
        }
	}
    //上传视频
    function videoChange(target) {
        var fileSize = 0;
        fileSize = target.files[0].size;
        var size = fileSize / 1024;
        size = size / 1024;
        if(size > 30){
           alert("视频不能大于30M");
           target.value="";
           return false;   //阻止submit提交
        }
        var name=target.value;
        var fileName = name.substring(name.lastIndexOf(".")+1).toLowerCase();
        if(fileName !="mp4"){
           alert("请选择文件格式上传(.mp4格式)！");
           target.value="";
           return false;   //阻止submit提交
        }
	}
 
 	function cancel(){
 		var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
        parent.layer.close(index);
 	}
      
	//添加或修改
	function insertProxyAr(){	
		var formData = new FormData($( "#add_proxy_ar" )[0]);  
		$("#button_id").attr('disabled',true);
		$.ajax({
			processData: false,
			contentType: false,
    		url:"${pageContext.request.contextPath }/admin/proxyQrCode/add",
    		data:formData,
    		type:"post",
            // 请求发送之前（发送请求前可修改XMLHttpRequest对象的函数，如添加自定义HTTP头。）。
            beforeSend:function(XMLHttpRequest){
                $("#doing").html("正在处理，请稍后···"); 
            }, 
    		success:function(data){
    			if(data.code == 0){
					layer.msg(data.message,{icon:1});
					setTimeout('cancel()',1000);
    			}else {
					layer.msg(data.message,{icon:0});
    			}
    		},
            // 请求完成后的回调函数 (请求成功或失败之后均调用)
            complete:function(XMLHttpRequest,textStatus){

                $("#doing").empty(); 
            }, 
            // 请求失败时调用此函数。
            error:function(XMLHttpRequest,textStatus,errorThrown){
                $("#doing").empty(); 
            } 
    	});
	}
	
	

	//获取行业
	function get_select_category(){
		var code = document.getElementById('category').value.replace(",","");
		$.get("/admin/category/getItems/"+code, function(result){
			var hangyeItem = $("#categoryId");
			hangyeItem.empty();
			var option0 = $("<option>").text('选择分类').val('');
			hangyeItem.append(option0);
			for(var i=0;i<result.length;i++) {
				if(i == 0){
					var option = $("<option>").text(result[i].name).val(result[i].id).attr("selected","selected");
				}else{
					var option = $("<option>").text(result[i].name).val(result[i].id);
				}
				hangyeItem.append(option);
			}
		});
	}
	</script>
</html>
